home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / Mentat / Mentat.java < prev    next >
Encoding:
Java Source  |  2001-06-23  |  9.3 KB  |  253 lines

  1. //
  2. //  Mentat.java
  3. //
  4.  
  5. /*
  6.     This file and its intellectual contents are considered property of the creator and are to be     treated as such with respect to use in other applications.  Any use of this software for any     purpose whatsoever must have explicit permission from the author of the file.  This code is     part of an ongoing development process for future possible products, and so is considered     private property.
  7.     Do not use without permission.  Author: Scott C. Ziegler <Aslan@Narnia.net>
  8. */
  9.  
  10.  
  11. import java.awt.*;
  12. import java.io.*;
  13. import javax.swing.*;
  14. import java.util.*;
  15.  
  16.  
  17. public class Mentat {
  18.  
  19.     public static final String LIST_COMMANDS = "?, about, addEntry, addEntryFromFile, removeEntry, lookupEntry, listEntries, changeName, saveArchive, loadArchive, Quit";
  20.     
  21.     // Members
  22.     
  23.     boolean             interactionMode;
  24.     boolean                touched;
  25.     
  26.     InteractionFrame     mentatFrame;
  27.     Archive                archie;
  28.     
  29.     FileInputStream        fis;
  30.     FileOutputStream    fos;
  31.  
  32.     // Constructors
  33.     
  34.     public Mentat() {
  35.         mentatFrame = new InteractionFrame();
  36.         archie = new Archive();
  37.         interactionMode = true;
  38.         }
  39.         
  40.     // Selectors
  41.     
  42.     public Archive getArchie() { return archie; }
  43.     public boolean getInteractionMode() { return interactionMode; }
  44.     
  45.     // Mutators
  46.     
  47.     public void setArchie(Archive newArchie) { newArchie = new Archive(); }
  48.     public void setInteractionMode(boolean mode) { interactionMode = mode; }
  49.     
  50.     // Methods
  51.     
  52.     public void mainline() {
  53.         String selector;
  54.         
  55.         while(true) {
  56.             selector = mentatFrame.getLine();
  57.             
  58.                  if (selector.compareTo("") == 0) {
  59.                     mentatFrame.printEndLine("Enter a command, Moron!");
  60.                     }
  61.             else if (selector.compareTo("help") == 0) {
  62.                     mentatFrame.printLine("Mentat Thufir Howat, at your service.");
  63.                     mentatFrame.print("Available commands: ");
  64.                     mentatFrame.printEndLine(LIST_COMMANDS);
  65.                     }
  66.             else if (selector.compareTo("Commands?") == 0 || selector.compareTo("?") == 0) {
  67.                     mentatFrame.printEndLine(LIST_COMMANDS);
  68.                     }
  69.             else if (selector.compareTo("about") == 0) {
  70.                     about();
  71.                     }
  72.             else if (selector.compareTo("addEntry") == 0) {
  73.                     addEntry();
  74.                     }
  75.             else if (selector.compareTo("addEntryFromFile") == 0) {
  76.                     addEntryFromFile();
  77.                     }
  78.             else if (selector.compareTo("removeEntry") == 0) {
  79.                     removeEntry();
  80.                     }
  81.             else if (selector.compareTo("lookupEntry") == 0) {    
  82.                     lookupEntry();
  83.                     }
  84.             else if (selector.compareTo("listEntries") == 0) {
  85.                     listEntries();
  86.                     }
  87.             else if (selector.compareTo("changeName") == 0) {
  88.                     changeName();
  89.                     }
  90.             else if (selector.compareTo("saveArchive") == 0) {
  91.                     saveArchive();
  92.                     }
  93.             else if (selector.compareTo("loadArchive") == 0) {
  94.                     loadArchive();
  95.                     }
  96.             else if (selector.compareTo("Quit") == 0) {    
  97.                     mentatFrame.printLine("Exiting Shell... Quiessence.");
  98.                     System.exit(0);
  99.                     break;
  100.                     }
  101.             else { // default case
  102.                     mentatFrame.printEndLine("The command \""+ selector +"\" was not recognized!");
  103.                     }
  104.                    }
  105.             }
  106.             
  107.     public void about() {
  108.         mentatFrame.printLine("Mentat for MacHack 2001");
  109.         mentatFrame.printLine("written by Scott C. Ziegler, 2000-2001");
  110.         mentatFrame.printLine("presented at MacHack 2001 on MacOS X");
  111.         mentatFrame.printLine("     This small application was developed during experimentations with the Java programming language and its Java Foundation Classes, including Swing.  This is for all those mental notes that always seem to get lost in the cobwebs behind all the deprecated APIs.  This app is dedicated to my friends from days of old when magick filled the aire, and to those who have ever felt the age-smoothed edges of a polyhedron as it decided the fate of some hapless character.");
  112.         mentatFrame.printEndLine("You never saw this fnord.");
  113.         }
  114.             
  115.     public void addEntry() {
  116.         String keyString = new String("");
  117.         String valueString = new String("");
  118.         
  119.         mentatFrame.printLine("Adding an Entry:");
  120.         
  121.         // key variable acquisition
  122.         keyString = getLineLoop("Enter the desired key (i.e. keyword)...", 
  123.                                 "That is an invalid key!");
  124.         // value variable acquisition
  125.         valueString = getLineLoop("Enter the desired value associated with this key...",
  126.                                     "That value is rather silly, don't you think?");
  127.         
  128.         archie.addEntry(keyString, valueString);
  129.         mentatFrame.printEndLine("Entry Added.");
  130.         touched = true;
  131.         
  132.         // if () { }  // check for entry's existence? then report?
  133.         
  134.         }
  135.         
  136.     public void addEntryFromFile() {
  137.         String filename = new String("");
  138.         String key = new String("");
  139.         mentatFrame.printLine("Adding and Entry from file:");
  140.         key = getLineLoop("Enter the desired key (i.e. keyword)...", 
  141.                                 "That is an invalid key!");
  142.         filename = getLineLoop("Enter the filename of the value for the entry.", 
  143.                                 "That is an invalid filename!");
  144.         try {
  145.             FileReader in = new FileReader(filename);
  146.             BufferedReader bf = new BufferedReader(in);
  147.             
  148.             archie.addEntry(key, bf.readLine());
  149.             mentatFrame.printEndLine("Entry Added.");
  150.             touched = true;
  151.             }
  152.         catch (IOException ioe) {
  153.             System.out.println("Error: " + ioe.toString());
  154.             }
  155.         }
  156.         
  157.     public void removeEntry() {
  158.         String key = new String("");
  159.         mentatFrame.printLine("Removing an Entry:");
  160.         key = getLineLoop("Enter the key of the entry to be deleted:", "Can't destroy nothing, silly!");
  161.         // Add are you sure dialog text.
  162.            archie.removeEntry(key);
  163.     mentatFrame.printEndLine("Entry removed.");
  164.            touched = true;
  165.         }
  166.     
  167.     public void lookupEntry() {
  168.         String key = new String("");
  169.         mentatFrame.printLine("Lookup an Entry:");
  170.         key = getLineLoop("Enter the key of the entry to be looked up:", 
  171.                             "That is not a valid entry fnord.");
  172.         mentatFrame.printEndLine(archie.lookupValue(key));
  173.         }
  174.         
  175.     public void listEntries() {
  176.         Enumeration e = archie.getArch().keys();
  177.         while(e.hasMoreElements()) {
  178.             mentatFrame.printLine((String)e.nextElement());
  179.             }
  180.         mentatFrame.printEndLine("There are " + archie.getArch().size() + " entries in the Archive.");
  181.         }
  182.     
  183.     public void changeName() {
  184.         String newName = new String("");
  185.         newName = getLineLoop("Enter the new name of the Archive.", "That's not very imaginative!");
  186.         archie.setName(newName);
  187.         mentatFrame.printEndLine("The new name is " + newName + " .");
  188.         touched = true;
  189.         }
  190.     
  191.     public void saveArchive() {
  192.         try {
  193.             String filename = new String("untitled.arc");
  194.             filename = getLineLoop("Enter the filename of the Archive file.",
  195.                                                             "That is not a valid filename.");
  196.             fos = new FileOutputStream(filename);
  197.             ObjectOutputStream oos = new ObjectOutputStream(fos);
  198.             archie.saveArch(oos);
  199.             mentatFrame.printEndLine("Archive Saved.fnord");
  200.             touched = false;
  201.             }
  202.         catch (FileNotFoundException fnfe) {
  203.             System.out.println("Error: " + fnfe.toString());
  204.             }
  205.         catch (IOException ioe) {
  206.             System.out.println("Error: " + ioe.toString());
  207.             }
  208.         }
  209.         
  210.     private void loadBody() {
  211.         try {
  212.             String filename = new String("untitled.arc");
  213.             filename = getLineLoop("Enter the filename of the Archive file.", "That is not a valid filename.");
  214.             fis = new FileInputStream(filename);
  215.             ObjectInputStream ois = new ObjectInputStream(fis);
  216.             archie.loadArch(ois);
  217.             touched = false;
  218.             mentatFrame.printEndLine("Archive Loaded.");
  219.             }
  220.         catch (FileNotFoundException fnfe) { System.out.println("Error: " + fnfe.toString()); }
  221.         catch (IOException ioe) { System.out.println("Error: " + ioe.toString()); }
  222.         }
  223.         
  224.     public void loadArchive() {
  225.         String answer = new String("");
  226.         if (touched) {
  227.             answer = getLineLoop("File not saved! Load anyways?", "Come Again?");
  228.             if (answer.compareTo("Y") == 0 || answer.compareTo("y") == 0
  229.                     || answer.compareTo("Yes") == 0 || answer.compareTo("yes") == 0) loadBody();
  230.             }
  231.         else loadBody();
  232.         }
  233.     
  234.        private String getLineLoop(String request, String error) {
  235.            String temp = new String();
  236.            while(true) {
  237.             mentatFrame.printEndLine(request);
  238.             temp = mentatFrame.getLine();
  239.             if (temp.compareTo("") == 0) {
  240.                 mentatFrame.printEndLine(error);
  241.                 continue;
  242.                 }
  243.                 break;
  244.             }
  245.         return temp;
  246.            }
  247.     
  248.     public static void main(String args[]) {
  249.         Mentat thufir = new Mentat();
  250.         thufir.mainline();
  251.         }
  252.     }
  253.